import unittest

import stackless

from display import ChannelDisplay
from charectors import BasicCharector as Charector

import world

from event import Event

class WorldTest(unittest.TestCase):

    def setUp(self):
        self.channel=stackless.channel()
        self.display=ChannelDisplay(self.channel)

        self.world=world.World(self.display)

    def testDestroy(self):
        '''Testing Destroying World... If it doesn't fail, it is very horrible'''

        self.world.channel.send(Event(subject='destroy'))

        self.world.sleepManager.sleep(0.1)
       
        self.assertEqual(self.channel.balance,0)


    def testCollisionEdge(self):
        '''Testing the collisions, Edge of the world now'''

        charector=Charector(self.world,(4,4))
        self.channel.receive()

        collisions=self.world.getCollisions(charector.rect,channel=charector.channel)

        self.assertEquals(len(collisions),1)
        self.assertEquals(collisions[0][0],self.world.channel)

        charector.remove()

        while self.channel.balance > 0:
            self.channel.receive()

    def testCollisionOther(self):
        '''Testing the collisions with another rect'''

        charector1=Charector(self.world,(10,10))
        charector2=Charector(self.world,(11,11))

        for ch1,ch2 in ((charector1,charector2),(charector2,charector1)):
        
            collisions=self.world.getCollisions(ch1.rect,channel=ch1.channel)
            
            self.assertEquals(len(collisions),1)
            self.assertEquals(collisions[0],(ch2.channel,ch2.rect))

    def testMultipleCollision(self):
        '''Testing the collisions of BOTH edge and other rect'''

        ch1=Charector(self.world,(4,4))
        ch2=Charector(self.world,(6,6))

        collisions=self.world.getCollisions(ch1.rect,channel=ch1.channel)

        self.assertEqual(len(collisions),2)

    def testRectAttribution(self):
       '''Test if world.rect is the Full Screen'''

       self.assertTrue(hasattr(self.world,'rect'),'world does not has an rect')

       self.assertEqual(self.world.rect,(0,0,self.display.maxx,self.display.maxy))


unittest.main()
